package com.android.kotlincoroutinemvvmdemo.view;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class RectPathAnimatorView2 extends View {
    private Paint mPaint0;
    private Paint mPaint1;
    private Path mPath;
    private Path targetPath;
    private Path compensatePath;
    private PathMeasure pathMeasure;
    private float mLength;
    private final float segmentLength = 100;
    private float mCurrentPosition = 0f;
    //设置起始位置
    private float mStartPosition = 0.1f;

    public RectPathAnimatorView2(Context context) {
        this(context, null);
    }

    public RectPathAnimatorView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RectPathAnimatorView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint0 = new Paint();
        mPaint0.setColor(Color.RED);
        mPaint0.setAntiAlias(true);
        mPaint0.setDither(true);
        mPaint0.setStrokeWidth(10);
        mPaint0.setPathEffect(new CornerPathEffect(10));
        mPaint0.setStyle(Paint.Style.STROKE);

        mPaint1 = new Paint();
        mPaint1.setColor(Color.BLUE);
        mPaint1.setAntiAlias(true);
        mPaint1.setDither(true);
        mPaint1.setStrokeWidth(10);
        mPaint1.setPathEffect(new CornerPathEffect(10));
        mPaint1.setStyle(Paint.Style.STROKE);
    }


    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        super.onSizeChanged(width, height, oldWidth, oldHeight);

        targetPath = new Path();
        compensatePath = new Path();

        //定义矩形路径
        mPath = new Path();
        mPath.addRect(mPaint0.getStrokeWidth(),
                mPaint0.getStrokeWidth(),
                width - mPaint0.getStrokeWidth(),
                height - mPaint0.getStrokeWidth(),
                Path.Direction.CW);
        mLength = new PathMeasure(mPath, false).getLength();

        pathMeasure = new PathMeasure(mPath, false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制整个矩形边框
        canvas.drawPath(mPath, mPaint0);

        float stop = ((mCurrentPosition + mStartPosition) * mLength) % mLength;
        targetPath.reset();
        pathMeasure.getSegment(stop, stop + segmentLength, targetPath, true);
        boolean condition = true;

        //100          85             15
        if (mLength - stop < segmentLength) {
            condition = false;
            //绘制线段补偿路径
            compensatePath.reset();
            compensatePath.moveTo(0F + mPaint1.getStrokeWidth(), (mLength - stop) + mPaint1.getStrokeWidth());
            compensatePath.lineTo(0F + mPaint1.getStrokeWidth(), 0F + mPaint1.getStrokeWidth());
            compensatePath.lineTo(segmentLength - (mLength - stop) + mPaint1.getStrokeWidth(), 0F + mPaint1.getStrokeWidth());
            canvas.drawPath(compensatePath, mPaint1);
        }

        //绘制线段路径
        if (condition) {
            canvas.drawPath(targetPath, mPaint1);
        }
    }

    public void startAnimation() {
        ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(20000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(animation -> {
            mCurrentPosition = (float) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();
    }
}
